/**
 * 打点模式
 */
export default function useDotting() {
  const dotting = ref(false);
  const positions = ref<number[]>([]);

  // 启动打点
  const startDotting = (callback: any, maxNumber?: number) => {
    const { cimInstance } = window;
    resetDotting();
    dotting.value = true;
    const dottingCallback = (newPositions: number[]) => {
      positions.value = newPositions;

      if (typeof callback === 'function') {
        callback(newPositions);
        resetDotting();
      }
    };
    cimInstance.api.drawPath(dottingCallback, maxNumber);
  };

  // 退出打点
  const stopDotting = () => {
    resetDotting();
  };

  // 重置状态
  const resetDotting = () => {
    const { cimInstance } = window;
    dotting.value = false;
    positions.value = [];
    cimInstance.api.cancelDraw();
  };

  return {
    dotting,
    positions,
    startDotting,
    stopDotting,
    resetDotting,
  };
}
